/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.general.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.Area;
import org.cip.esymons.general.be.Cargo;
import org.cip.esymons.general.be.Empleado;

/**
 *
 * @author ADMINISTRADOR
 */
@Stateless
public class CargoFacade implements CargoFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(Cargo cargo) {
        em.persist(cargo);
    }

    public void edit(Cargo cargo) {
        em.merge(cargo);
    }

    public void remove(Cargo cargo) {
        em.remove(em.merge(cargo));
    }

    public Cargo find(Object id) {
        return em.find(Cargo.class, id);
    }

    public List<Cargo> findAll() {
        return em.createQuery("select object(o) from Cargo as o order by o.carDescripcion").getResultList();
    }

    public List<Cargo> listarXAreayEmpleado(Area area, Empleado empl) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("select c.cargo from CargoEmpleado c inner Join c.empleado o");
            sb.append(" where o=?1 and c.cargo.fkAreCodigo=?2 ");

            q = em.createQuery(sb.toString());

            q.setParameter(1, empl);
            q.setParameter(2, area);
            return q.getResultList();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Cargo> listaCargoPorArea(Integer area) {

        return em.createQuery("select object(o) from Cargo as o where o.fkAreCodigo.areCodigo=?1 order by o.carDescripcion").setParameter(1, area).getResultList();
    }

    //con objeto
    public List<Cargo> listaCargosPorAreas(Area area) {

        return em.createQuery("select object(o) from Cargo as o where o.fkAreCodigo=?1 order by o.carDescripcion").setParameter(1, area).getResultList();
    }

    public List<Cargo> listarXArea(Area area) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select d From Cargo d ");

            sb.append(" Where d.area = ?2");

            q = em.createQuery(sb.toString());

            q.setParameter(2, area);

            return q.getResultList();
        } catch (Exception ex) {
            return null;
        }
    }

    public Cargo buscar(String nombre) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select d From Cargo d ");

            sb.append(" Where d.carDescripcion = ?1");

            q = em.createQuery(sb.toString());

            q.setParameter(1, nombre);

            return (Cargo) q.getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public Cargo buscarXArea(String desc, Area area) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select d From Cargo d ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where d.carDescripcion = ?1 and d.area = ?2");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, desc);
                q.setParameter(2, area);
            } else {
                return null;
            }
            return (Cargo) q.getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public Cargo buscarXTransaccion(Long trans) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append(" select o.carCodigo");
            sb.append(" from Operacion o inner Join o.fkTraCodigo t");
            sb.append(" where t.traCodigo=?1 and o.opeDescripcion=?2");

            q = em.createQuery(sb.toString());


            q.setParameter(1, trans);
            q.setParameter(2, "Inicio");

            return (Cargo) q.getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Cargo> paginadorCargo(int cantidad, int posicion, String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select d From Cargo d ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where d.carDescripcion like ?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, "%" + desc + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorCargo(String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select COUNT(d) From Cargo d ");

            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where d.carDescripcion =?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, desc);
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }
}
